const user = require("../sql/user");
const sql = require("../sql/index");
exports.login = function () {
    return (req, res, next) => {
        sql.get(user, { username: req.query.username }, data => {
            // console.log(data)
            if (data.length > 0) {
                if (data[0].password === req.query.password) {
                    // 登录成功后
                    // 存储状态：session
                    req.session.userid = data[0].userid;
                    s = data[0].userid
                    console.log(s);
                    sql.set(user, {
                        username: req.query.username
                    }, {
                        isLogin: 1
                    }, () => {
                        res.send(JSON.stringify({
                            code: 1,
                            msg: "登录成功",
                            age: data[0].userid,
                        }));
                    })
                } else {
                    res.send(JSON.stringify({
                        code: 2,
                        msg: "登录失败，密码不对"
                    }));
                }
            } else {
                res.send(JSON.stringify({
                    code: 0,
                    msg: "登录失败，用户名不存在"
                }));
            }
        })
    }
}
